<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/29
 * Time: 17:12
 */

namespace Common\Model;


use Think\Model;

class RoleUsers extends Model
{
    private $users_id;
    private $roles_id;

    protected $tableName = 'role_users';

    /**
     * @param $users_id
     * @return null|RoleUsers
     */
    public static function findBuUserId($users_id)
    {
        $mod = new RoleUsers();
        $role = $mod->where('users_id = ' . $users_id)->select();
        if (!empty($role)) {
            $mod->users_id = $role[0]['users_id'];
            $mod->roles_id = $role[0]['roles_id'];
            return $mod;
        } else {
            return null;
        }
    }

    /**
     * @return Role
     */
    public function role()
    {
        return new Role($this->roles_id);
    }

    public static function createUserRole($users_id, $roles_id)
    {
        $mod = new static();
        return $mod->add([
            'users_id' => $users_id,
            'roles_id' => $roles_id,
            'is_enable' => 1
        ]);
    }

    /**
     * 更新用的角色
     *
     * @param $users_id
     * @param $roles_id
     * @return bool
     */
    public static function update($users_id, $roles_id)
    {
        $rolesUser = M('role_users');
        $rolesUser->roles_id = $roles_id;
        return $rolesUser->where('users_id = ' . $users_id)->save();
    }
}